<template>
    <section class="form-container">
        <el-form :inline="true" size="mini" class="form-filter">
            <el-form-item>
                <el-button @click="showInsert(0)" icon="el-icon-plus">添加省份</el-button>
            </el-form-item>
        </el-form>
        <div class="form-content">
            <el-table
                v-loading="loading"
                style="width: 100%"
                :data="dataSource"
                border
                stripe
                lazy
                :load="load"
                row-key="code"
                ref="table1"
                :header-cell-style = "headerCellStyle"
            >
                <el-table-column prop="name" label="名称"/>
                <el-table-column prop="code" label="编码"/>
                <el-table-column prop="" label="中心点">
                    <template #default="scope">
                        {{ scope.row.lng }}<span v-if="scope.row.lng && scope.row.lat">,</span>{{ scope.row.lat }}
                    </template>
                </el-table-column>
                <el-table-column prop="levelName" label="级别" width="80px"/>

                <el-table-column prop="updateName" label="更新人" width="80px"/>
                <el-table-column prop="updateTime" label="更新时间" width="160px"/>

                <el-table-column fixed="right" label="操作" width="260">
                    <template #default="scope">
                        <el-button size="mini" style="margin:0 0px 10px 10px;" @click="showInsert(scope.row.code)">添加子级
                        </el-button>

                        <el-button size="mini" style="margin:0 0px 10px 10px;" @click="showUpdate(scope.row)">编辑
                        </el-button>

                        <el-popconfirm title="确定删除吗？" @onConfirm="handleDelete(scope.row)">
                            <el-button slot="reference" size="mini" style="margin:0 0px 10px 10px;">删除
                            </el-button>
                        </el-popconfirm>
                    </template>
                </el-table-column>
            </el-table>
            <Update ref="updateRef" @refresh="reloadList"/>
        </div>
    </section>
</template>
<script>
import { RegionApi } from '@/api/system/RegionApi'
import Update from './Update.vue'

export default {
    name: 'region',
    components: {Update},
    data() {
        return {
            dataSource: [],
            coordinate: {column: -1, row: -1},
            loading: false,
            headerCellStyle:{
                "background-color":"#F2F5FC"
            }
        }
    },
    created() {
        this.reloadList(0)
    },
    methods: {
        reloadList(parentCode) {
            RegionApi.getChildList(parentCode).then((response) => {
                if (parentCode === 0) {
                    this.dataSource = response
                } else {
                    this.$set(this.$refs.table1.store.states.lazyTreeNodeMap, parentCode, response)
                }
            })
        },
        //展开子类
        load(tree, treeNode, resolve) {
            RegionApi.getChildList(tree.code).then((response) => {
                resolve(response)
            })
        },

        // 添加
        showInsert(parentCode) {
            this.$refs['updateRef'].showUpdate({'parentCode': parentCode})
        },
        // 修改
        showUpdate(data) {
            this.$refs['updateRef'].showUpdate(data)
        },
        // 删除
        handleDelete(row) {
            RegionApi.delete(row.id).then((response) => {
                this.reloadList(row.parentCode)
            })
        },
    }
}

</script>

<style>

</style>
